/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IFSv2.Forms.Main;

import IFSv2.Connection.DBConnection;
import IFSv2.Forms.frmLogin;
import IFSv2.Models.mdlCurrency;
import IFSv2.Models.mdlCurrentOperation;
import IFSv2.Models.mdlLogin;
import IFSv2.Models.mdlMain;
import IFSv2.Operations.clsMain;
import IFSv2.Operations.clsTab;
import IFSv2.Util.ListRenderer;
import IFSv2.Util.utilErrorHandler;
import IFSv2.Util.utilSystemPath;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;

/**
 *
 * @author TIMSDC
 */
public class frmMain extends javax.swing.JFrame {
           
    private clsMain oclsMain;
    
    javax.swing.JList lstFinancials;
    javax.swing.JList lstMatMgt;
    javax.swing.JList lstSupplyChain;
    javax.swing.JList lstPropMgt;
    javax.swing.JList blank; 
    
    public frmMain() {
        oclsMain = new clsMain();
        

        setIconImage(utilSystemPath.getFormImage(this.getClass().getResource("/IFSv2/Images/timlogo.png")));
        setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);
        
        initComponents();

        loadMenuContents();
        updateProgramInfo();

        
    }

    private void updateProgramInfo(){
        oclsMain.updateInfoBar();
    }
    
    public static void changeCurrency(String currencyCode){
        lblCurrCurrency.setText(mdlCurrency.currencyCode);
    }
    
        
    private void loadMenuContents(){
        
        ListRenderer listRenderer = new ListRenderer();
        
        lstFinancials = new javax.swing.JList(oclsMain.getMenuList("financials"));
        lstMatMgt = new javax.swing.JList(oclsMain.getMenuList("materialsmanagement"));
        lstSupplyChain = new javax.swing.JList(oclsMain.getMenuList("supplychain"));
        lstPropMgt = new javax.swing.JList(oclsMain.getMenuList("propertymanagement"));
        blank = new javax.swing.JList();
                
        lstFinancials.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstMatMgt.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstSupplyChain.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstPropMgt.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
         
        lstFinancials.setCellRenderer(listRenderer);
        lstMatMgt.setCellRenderer(listRenderer);
        lstSupplyChain.setCellRenderer(listRenderer);
        lstPropMgt.setCellRenderer(listRenderer);
        
        List<javax.swing.JList> list = new ArrayList();
        list.add(lstFinancials);
        list.add(lstMatMgt);
        list.add(lstSupplyChain);
        list.add(lstPropMgt);
        
        for(final javax.swing.JList lstItem : list){
            lstItem.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    loadPanelfromList(lstItem);
                }
            });
        }
        jOutlookBar1.add("Financials", lstFinancials);
        jOutlookBar1.add("Materials Management", lstMatMgt);
        jOutlookBar1.add("Supply Chain", lstSupplyChain);
        jOutlookBar1.add("Property Management", lstPropMgt);
        jOutlookBar1.add(" ", blank);
        jOutlookBar1.setSelectedComponent(blank);
        
        // <editor-fold defaultstate="collapsed" desc="Menu Bar Items(titignan ko to later)">
        //Financial
        String[] lstFinancial = oclsMain.getMenuList("Financials");
        for(int i=0; i< lstFinancial.length; i++){
            if(lstFinancial[i].equals("Reports") || lstFinancial[i].equals("Lists")){
                menuFinancials.addSeparator();
                javax.swing.JMenuItem itemFin = new javax.swing.JMenuItem(lstFinancial[i]);
                menuFinancials.add(itemFin);
                menuFinancials.addSeparator();
            }else{
                javax.swing.JMenuItem itemFin = new javax.swing.JMenuItem(lstFinancial[i]);
                menuFinancials.add(itemFin);
            }
        }
       
        //Materials Management
        String[] lstMat = oclsMain.getMenuList("Materials Management");
        for(int i=0; i< lstMat.length; i++){
            if(lstMat[i].equals("Reports") || lstMat[i].equals("Lists")){
                menuMaterials.addSeparator();
                javax.swing.JMenuItem itemMat = new javax.swing.JMenuItem(lstMat[i]);
                menuMaterials.add(itemMat);
                menuMaterials.addSeparator();
            }else{
                javax.swing.JMenuItem itemMat = new javax.swing.JMenuItem(lstMat[i]);
                menuMaterials.add(itemMat);
            }
        }
        //Supply Chain
        String[] lstSChain = oclsMain.getMenuList("Supply Chain");
        for(int i=0; i< lstSChain.length; i++){
            
            if(lstSChain[i].equals("Reports") || lstSChain[i].equals("Lists")){
                menuSupplyChain.addSeparator();
                javax.swing.JMenuItem itemSC = new javax.swing.JMenuItem(lstSChain[i]);
                menuSupplyChain.add(itemSC);
            }else{
                javax.swing.JMenuItem itemMat = new javax.swing.JMenuItem(lstSChain[i]);
                menuSupplyChain.add(itemMat);
            }
            
        }
        
        //Property Management
        String[] lstPropertyMgt = oclsMain.getMenuList("Property Management");
        for(int i=0; i< lstPropertyMgt.length; i++){
            
            if(lstPropertyMgt[i].equals("Reports") || lstPropertyMgt[i].equals("Lists")){
                menuProperty.addSeparator();
                javax.swing.JMenuItem itemPM = new javax.swing.JMenuItem(lstPropertyMgt[i]);
                menuProperty.add(itemPM);
            }else{
                javax.swing.JMenuItem itemPM = new javax.swing.JMenuItem(lstPropertyMgt[i]);
                menuProperty.add(itemPM);
            }
            
        }
       //</editor-fold>

                
    }
        
    public void loadPanelfromList(javax.swing.JList list){
    
        String selectedItem = "";
                
        try{
            selectedItem = list.getSelectedValue().toString(); 
            if (!(mdlMain.tabMap.containsKey(selectedItem))){
                clsMain.openTab(selectedItem, oclsMain.loadPanel(oclsMain.programMap.get(selectedItem)));
                mdlCurrentOperation.activeOperation.put(((clsTab)menuTabbedPane.getComponentAt(menuTabbedPane.getSelectedIndex())).getTitle(),"Add");
            }
            menuTabbedPane.setSelectedComponent(mdlMain.tabMap.getForward(selectedItem));
            
        }catch (Exception e){}
        list.clearSelection();  
        menuTabbedPane.requestFocus();
    }

    private void openSetting(){
        /*
         * Note: this is using java reflection, setFieldSecurity must be declared
         * on each form loaded on the tabbedpane to use this feature
         */
        try{
            menuTabbedPane.getSelectedComponent().getClass().getDeclaredMethod("setFieldSecurity", null).invoke(menuTabbedPane.getSelectedComponent(), null);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException ex) {
            utilErrorHandler.printError(":Settings not available on active tab", "clsOperations", "getConfigFile");
        } 
    }
        
    private void tabChanged(){
        /*
         * Note: this is using java reflection, reloadToolBar must be declared
         * on each form loaded on the tabbedpane
         */
        try {
            menuTabbedPane.getSelectedComponent().getClass().getDeclaredMethod("reloadToolBar", null).invoke(menuTabbedPane.getSelectedComponent(), null);        
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
            Logger.getLogger(frmMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        menuPanel = new javax.swing.JPanel();
        jOutlookBar1 = new com.l2fprod.common.swing.JOutlookBar();
        menuTabbedPane = new javax.swing.JTabbedPane();
        toolBar = new MainToolBar();
        infoBar = new javax.swing.JToolBar();
        lblDBUsed = new javax.swing.JLabel();
        lblCurrDBUsed = new javax.swing.JLabel();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(10, 0), new java.awt.Dimension(10, 0), new java.awt.Dimension(10, 32767));
        jSeparator2 = new javax.swing.JToolBar.Separator();
        filler2 = new javax.swing.Box.Filler(new java.awt.Dimension(10, 0), new java.awt.Dimension(10, 0), new java.awt.Dimension(10, 32767));
        lblDBConn = new javax.swing.JLabel();
        lblCurrDB = new javax.swing.JLabel();
        filler3 = new javax.swing.Box.Filler(new java.awt.Dimension(10, 0), new java.awt.Dimension(10, 0), new java.awt.Dimension(10, 32767));
        jSeparator1 = new javax.swing.JToolBar.Separator();
        filler4 = new javax.swing.Box.Filler(new java.awt.Dimension(10, 0), new java.awt.Dimension(10, 0), new java.awt.Dimension(10, 32767));
        lblUsername = new javax.swing.JLabel();
        lblCurrUsername = new javax.swing.JLabel();
        filler7 = new javax.swing.Box.Filler(new java.awt.Dimension(10, 0), new java.awt.Dimension(10, 0), new java.awt.Dimension(10, 32767));
        jSeparator3 = new javax.swing.JToolBar.Separator();
        filler6 = new javax.swing.Box.Filler(new java.awt.Dimension(10, 0), new java.awt.Dimension(10, 0), new java.awt.Dimension(10, 32767));
        lblCurrency = new javax.swing.JLabel();
        lblCurrCurrency = new javax.swing.JLabel();
        filler5 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        jLabel1 = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        menuFile = new javax.swing.JMenu();
        itmLogout = new javax.swing.JMenuItem();
        itmExit = new javax.swing.JMenuItem();
        menuModule = new javax.swing.JMenu();
        itmFinancials = new javax.swing.JMenuItem();
        itmMaterialsManagement = new javax.swing.JMenuItem();
        itmSupplyChain = new javax.swing.JMenuItem();
        itmPropertyManagement = new javax.swing.JMenuItem();
        menuFinancials = new javax.swing.JMenu();
        menuMaterials = new javax.swing.JMenu();
        menuSupplyChain = new javax.swing.JMenu();
        menuProperty = new javax.swing.JMenu();
        menuTools = new javax.swing.JMenu();
        itemSettings = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        menuHelp = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Integrated Financial System v1.0");

        menuPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Modules", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(102, 102, 102)));

        jOutlookBar1.setSelectedIndex(jOutlookBar1.getTabCount()-1);

        javax.swing.GroupLayout menuPanelLayout = new javax.swing.GroupLayout(menuPanel);
        menuPanel.setLayout(menuPanelLayout);
        menuPanelLayout.setHorizontalGroup(
            menuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(menuPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jOutlookBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                .addContainerGap())
        );
        menuPanelLayout.setVerticalGroup(
            menuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, menuPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jOutlookBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
                .addContainerGap())
        );

        menuTabbedPane.setBackground(new java.awt.Color(168, 168, 168));
        menuTabbedPane.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                menuTabbedPaneStateChanged(evt);
            }
        });

        toolBar.setFloatable(false);
        toolBar.setPreferredSize(new java.awt.Dimension(14, 25));

        infoBar.setBackground(new java.awt.Color(204, 204, 204));
        infoBar.setFloatable(false);

        lblDBUsed.setText("  Database Used:  ");
        infoBar.add(lblDBUsed);
        infoBar.add(lblCurrDBUsed);
        infoBar.add(filler1);
        infoBar.add(jSeparator2);
        infoBar.add(filler2);

        lblDBConn.setText("Database Connection:  ");
        infoBar.add(lblDBConn);
        infoBar.add(lblCurrDB);
        infoBar.add(filler3);
        infoBar.add(jSeparator1);
        infoBar.add(filler4);

        lblUsername.setText("User:  ");
        infoBar.add(lblUsername);
        infoBar.add(lblCurrUsername);
        infoBar.add(filler7);
        infoBar.add(jSeparator3);
        infoBar.add(filler6);

        lblCurrency.setText("Current Currency:  ");
        infoBar.add(lblCurrency);
        infoBar.add(lblCurrCurrency);
        infoBar.add(filler5);

        jLabel1.setText("Copyright 2012  ");
        infoBar.add(jLabel1);

        menuFile.setText("File");

        itmLogout.setText("Logout");
        itmLogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itmLogoutActionPerformed(evt);
            }
        });
        menuFile.add(itmLogout);

        itmExit.setText("Exit");
        itmExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itmExitActionPerformed(evt);
            }
        });
        menuFile.add(itmExit);

        menuBar.add(menuFile);

        menuModule.setText("Modules");

        itmFinancials.setText("Financials");
        itmFinancials.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itmFinancialsActionPerformed(evt);
            }
        });
        menuModule.add(itmFinancials);

        itmMaterialsManagement.setText("Materials Management");
        itmMaterialsManagement.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itmMaterialsManagementActionPerformed(evt);
            }
        });
        menuModule.add(itmMaterialsManagement);

        itmSupplyChain.setText("Supply Chain");
        menuModule.add(itmSupplyChain);

        itmPropertyManagement.setText("Property Management");
        menuModule.add(itmPropertyManagement);

        menuBar.add(menuModule);

        menuFinancials.setText("Financials");

        menuFinancials.setVisible(false);

        menuBar.add(menuFinancials);

        menuMaterials.setText("Materials");

        menuMaterials.setVisible(false);

        menuBar.add(menuMaterials);

        menuSupplyChain.setText("Supply Chain");

        menuSupplyChain.setVisible(false);

        menuBar.add(menuSupplyChain);

        menuProperty.setText("Property");

        menuProperty.setVisible(false);

        menuBar.add(menuProperty);

        menuTools.setText("Tools");

        itemSettings.setText("Form Settings");
        itemSettings.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemSettingsActionPerformed(evt);
            }
        });
        menuTools.add(itemSettings);

        jMenuItem1.setText("Upload Excel");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        menuTools.add(jMenuItem1);

        jMenuItem3.setText("Report Options");
        menuTools.add(jMenuItem3);

        menuBar.add(menuTools);

        menuHelp.setText("Help");

        jMenuItem2.setText("About");
        menuHelp.add(jMenuItem2);

        menuBar.add(menuHelp);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(toolBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(menuPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(menuTabbedPane)
                .addGap(10, 10, 10))
            .addComponent(infoBar, javax.swing.GroupLayout.DEFAULT_SIZE, 877, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(toolBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(menuPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(menuTabbedPane))
                .addGap(6, 6, 6)
                .addComponent(infoBar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void itemSettingsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemSettingsActionPerformed
        openSetting();
    }//GEN-LAST:event_itemSettingsActionPerformed

    private void itmExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itmExitActionPerformed
        // <editor-fold defaultstate="collapsed" desc="Exit Action">
        int ans = javax.swing.JOptionPane.showConfirmDialog(this, "Exit Program?", "Exit IFS", javax.swing.JOptionPane.OK_CANCEL_OPTION);
        if(ans == javax.swing.JOptionPane.OK_OPTION){
            System.exit(1);
        }
        //</editor-fold>
    }//GEN-LAST:event_itmExitActionPerformed

    private void itmFinancialsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itmFinancialsActionPerformed
        menuFinancials.setVisible(true);
    }//GEN-LAST:event_itmFinancialsActionPerformed

    private void itmLogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itmLogoutActionPerformed
        // <editor-fold defaultstate="collapsed" desc="Logout Action">
        String message;
        if(menuTabbedPane.getTabCount() > 1){
            message = "Quit application. Unsaved changes on active tabs will be lost. \nDo you want to continue?";
        }else{
            message = "Quit application?";
        }
        final int result = JOptionPane.showConfirmDialog(this,message,"Logoff",JOptionPane.YES_NO_OPTION);
        if(result == JOptionPane.YES_OPTION){
            this.dispose();
            try {
                DBConnection.getActiveConnection().close();
            } catch (SQLException ex) {
                Logger.getLogger(frmMain.class.getName()).log(Level.SEVERE, null, ex);
            }
            final frmLogin login = new frmLogin();
            login.setVisible(true);
        }
        mdlLogin.clearAll();
        //</editor-fold>
    }//GEN-LAST:event_itmLogoutActionPerformed

    private void menuTabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_menuTabbedPaneStateChanged
        tabChanged();
    }//GEN-LAST:event_menuTabbedPaneStateChanged

    private void itmMaterialsManagementActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itmMaterialsManagementActionPerformed
        menuMaterials.setVisible(true);
    }//GEN-LAST:event_itmMaterialsManagementActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        clsMain.openUtilUploadExcel();
    }//GEN-LAST:event_jMenuItem1ActionPerformed
    //<editor-fold desc="Netbeans Code" defaultstate="collapsed">
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.Box.Filler filler1;
    private javax.swing.Box.Filler filler2;
    private javax.swing.Box.Filler filler3;
    private javax.swing.Box.Filler filler4;
    private javax.swing.Box.Filler filler5;
    private javax.swing.Box.Filler filler6;
    private javax.swing.Box.Filler filler7;
    private javax.swing.JToolBar infoBar;
    private javax.swing.JMenuItem itemSettings;
    private javax.swing.JMenuItem itmExit;
    private javax.swing.JMenuItem itmFinancials;
    private javax.swing.JMenuItem itmLogout;
    private javax.swing.JMenuItem itmMaterialsManagement;
    private javax.swing.JMenuItem itmPropertyManagement;
    private javax.swing.JMenuItem itmSupplyChain;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private com.l2fprod.common.swing.JOutlookBar jOutlookBar1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    public static javax.swing.JLabel lblCurrCurrency;
    public static javax.swing.JLabel lblCurrDB;
    public static javax.swing.JLabel lblCurrDBUsed;
    public static javax.swing.JLabel lblCurrUsername;
    private javax.swing.JLabel lblCurrency;
    private javax.swing.JLabel lblDBConn;
    private javax.swing.JLabel lblDBUsed;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenu menuFile;
    private javax.swing.JMenu menuFinancials;
    private javax.swing.JMenu menuHelp;
    private javax.swing.JMenu menuMaterials;
    private javax.swing.JMenu menuModule;
    private javax.swing.JPanel menuPanel;
    private javax.swing.JMenu menuProperty;
    private javax.swing.JMenu menuSupplyChain;
    public static javax.swing.JTabbedPane menuTabbedPane;
    private javax.swing.JMenu menuTools;
    public static MainToolBar toolBar;
    /*
    public static javax.swing.JToolBar toolBar;
    */
    // End of variables declaration//GEN-END:variables
    //</editor-fold>


}